package 所有的题类.ZA.链表;

import com.kiwi.nixu.common.LNode;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

public class 两数相加 {

    public static void main(String[] args) {
        LNode lNode1 = arrayToLNode(Arrays.asList(5, 4, 3, 4, 5, 6, 7, 8));
        LNode lNode2 = arrayToLNode(Arrays.asList(5, 5, 5));
        LNode lNode = addTwoNumbers(lNode1, lNode2);
        System.out.println(lNode.data);
    }
    public static LNode arrayToLNode(List<Integer> list){
        LNode lNode = new LNode(list.get(0));
        LNode cur=lNode;
        for (int i = 1; i < list.size(); i++) {
            LNode tmp=new LNode(list.get(i));
            cur.next=tmp;
            cur=tmp;
        }
        return lNode;
    }

    public static LNode addTwoNumbers(LNode l1, LNode l2) {
        LNode tmp1=l1;
        LNode tmp2=l2;
        int jinwei=0;
        LNode linked = new LNode(-1);
        LNode cur=linked;
        while(tmp2!=null || tmp1!=null){
            int tmpval1=0;
            int tmpval2=0;
            if(tmp1!=null){
                tmpval1=tmp1.data;
            }
            if(tmp2!=null){
                tmpval2=tmp2.data;
            }
            int c=tmpval1+tmpval2+jinwei;
            LNode tmp=null;
            if(c>=10){
                jinwei=c%10;
                tmp=new LNode(jinwei);
                jinwei=1;
            }else{
                tmp=new LNode(c);
                jinwei=0;
            }
            cur.next=tmp;
            cur=tmp;
            if(tmp2!=null){
                tmp2=tmp2.next;
            }
            if(tmp1!=null){
                tmp1=tmp1.next;
            }
        }
        return linked;
    }
}
